package com.sunstar.registered.hosp.controller.api;

import com.sunstar.registered.common.exception.RegisteredException;
import com.sunstar.registered.common.helper.HttpRequestHelper;
import com.sunstar.registered.common.result.Result;
import com.sunstar.registered.common.result.ResultCodeEnum;
import com.sunstar.registered.common.utils.MD5;
import com.sunstar.registered.hosp.service.DepartmentSerive;
import com.sunstar.registered.hosp.service.HospitalService;
import com.sunstar.registered.hosp.service.HospitalSetService;
import com.sunstar.registered.hosp.service.ScheduleService;
import com.sunstar.registered.model.hosp.Department;
import com.sunstar.registered.model.hosp.Hospital;
import com.sunstar.registered.model.hosp.Schedule;
import com.sunstar.registered.vo.hosp.DepartmentQueryVo;
import com.sunstar.registered.vo.hosp.ScheduleQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.data.domain.Page;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentSerive departmentSerive;

    @Autowired
    private ScheduleService scheduleService;

    //上传医院
    @PostMapping("/saveHospital")
    public Result saveHosp(HttpServletRequest request) {
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //1.获取医院系统传递过来的签名，签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");
        //2.根据传递过来的医院编码，查询数据库的签名
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3.把数据库查询签名进行MD5加密
        String encrypt = MD5.encrypt(signKey);
        //4.判断签名是否一致
        if (!hospSign.equals(encrypt)) {
            throw new RegisteredException(ResultCodeEnum.SIGN_ERROR);
        }

        //传输过程中“+”转换为了“ ”，因此需要转换回来
        String logoData = (String) paramMap.get("logoData");
        logoData = logoData.replaceAll(" ", "+");
        paramMap.put("logoData", logoData);

        //调用service方法实现
        hospitalService.save(paramMap);
        return Result.ok();
    }

    //查询医院
    @PostMapping("hospital/show")
    public Result getHosp(HttpServletRequest request) {
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //1.获取医院系统传递过来的签名，签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");
        //2.根据传递过来的医院编码，查询数据库的签名
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3.把数据库查询签名进行MD5加密
        String encrypt = MD5.encrypt(signKey);
        //4.判断签名是否一致
        if (!hospSign.equals(encrypt)) {
            throw new RegisteredException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service方法实现
        Hospital hospital = hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }

    //上传科室
    @PostMapping("/saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //1.获取医院系统传递过来的签名，签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");
        //2.根据传递过来的医院编码，查询数据库的签名
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3.把数据库查询签名进行MD5加密
        String encrypt = MD5.encrypt(signKey);
        //4.判断签名是否一致
        if (!hospSign.equals(encrypt)) {
            throw new RegisteredException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service方法实现
        departmentSerive.save(paramMap);
        return Result.ok();
    }

    //查询科室
    @PostMapping("/department/list")
    public Result findDepartment(HttpServletRequest request) {
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //当前页和每页记录数
        String page = (String) paramMap.get("page");
        String limit = (String) paramMap.get("limit");
        Integer pageSize = StringUtils.isEmpty(page) ? 1 : Integer.valueOf(page);
        Integer limitSize = StringUtils.isEmpty(limit) ? 1 : Integer.valueOf(limit);

        //获取医院编号和科室编号
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");

        //1.获取医院系统传递过来的签名，签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");
        //2.根据传递过来的医院编码，查询数据库的签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3.把数据库查询签名进行MD5加密
        String encrypt = MD5.encrypt(signKey);
        //4.判断签名是否一致
        if (!hospSign.equals(encrypt)) {
            throw new RegisteredException(ResultCodeEnum.SIGN_ERROR);
        }

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        departmentQueryVo.setDepcode(depcode);

        //调用service方法实现
        Page<Department> pageModel = departmentSerive.findPageDepartment(pageSize, limitSize, departmentQueryVo);
        return Result.ok(pageModel);
    }

    //删除科室
    @PostMapping("/department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //医院编号和科室编号
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");

        //1.获取医院系统传递过来的签名，签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");
        //2.根据传递过来的医院编码，查询数据库的签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3.把数据库查询签名进行MD5加密
        String encrypt = MD5.encrypt(signKey);
        //4.判断签名是否一致
        if (!hospSign.equals(encrypt)) {
            throw new RegisteredException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service方法实现
        departmentSerive.remove(hoscode, depcode);
        return Result.ok();
    }

    //上传排班
    @PostMapping("/saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //1.获取医院系统传递过来的签名，签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");
        //2.根据传递过来的医院编码，查询数据库的签名
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3.把数据库查询签名进行MD5加密
        String encrypt = MD5.encrypt(signKey);
        //4.判断签名是否一致
        if (!hospSign.equals(encrypt)) {
            throw new RegisteredException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service方法实现
        scheduleService.save(paramMap);
        return Result.ok();
    }

    //查询排班
    @PostMapping("/schedule/list")
    public Result findSchedule(HttpServletRequest request) {
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //当前页和每页记录数
        String page = (String) paramMap.get("page");
        String limit = (String) paramMap.get("limit");
        Integer pageSize = StringUtils.isEmpty(page) ? 1 : Integer.valueOf(page);
        Integer limitSize = StringUtils.isEmpty(limit) ? 1 : Integer.valueOf(limit);

        //获取医院编号和科室编号
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");

        //1.获取医院系统传递过来的签名，签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");
        //2.根据传递过来的医院编码，查询数据库的签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3.把数据库查询签名进行MD5加密
        String encrypt = MD5.encrypt(signKey);
        //4.判断签名是否一致
        if (!hospSign.equals(encrypt)) {
            throw new RegisteredException(ResultCodeEnum.SIGN_ERROR);
        }

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);

        //调用service方法实现
        Page<Schedule> pageModel = scheduleService.findPageSchedule(pageSize, limitSize, scheduleQueryVo);
        return Result.ok(pageModel);
    }

    //删除排班
    @PostMapping("/schedule/remove")
    public Result removeSchedule(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //医院编号和排班编号
        String hoscode = (String) paramMap.get("hoscode");
        String hosScheduleId = (String) paramMap.get("hosScheduleId");

        //1.获取医院系统传递过来的签名，签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");
        //2.根据传递过来的医院编码，查询数据库的签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3.把数据库查询签名进行MD5加密
        String encrypt = MD5.encrypt(signKey);
        //4.判断签名是否一致
        if (!hospSign.equals(encrypt)) {
            throw new RegisteredException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service方法实现
        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();
    }

}
